package person.bing.fbcloud.leetcode;

/**
 * @author: mengfanbing
 * @date: 2020/8/6 9:31
 */
public class Q21 {
    public static void main(String[] args) {

    }
    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null){
            return l2;
        }
        if (l2 == null){
            return l1;
        }
        ListNode ans = new ListNode(-1);
        ListNode ansTemp = ans;
        while (l1 != null || l2 != null){
            if (l1 != null && l2 != null){
                int val1 = l1.val;
                int val2 = l2.val;
                if (val1 <= val2){
                    ansTemp = ansTemp.next = l1;
                    l1 = l1.next;
                }else {
                    ansTemp = ansTemp.next = l2;
                    l2 = l2.next;
                }
            }else if (l1 != null && l2 == null){
                ansTemp = ansTemp.next = l1;
                l1 = l1.next;
            }else if (l1 == null && l2 != null){
                ansTemp = ansTemp.next = l2;
                l2 = l2.next;
            }
        }
        return ans.next;
    }


    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}
